var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, app = getApp();

function compareV(t, a) {
    if (t && a) {
        for (var o = t.split("."), e = a.split("."), n = Math.min(o.length, e.length), i = 0, s = 0; i < n && 0 == (s = parseInt(o[i]) - parseInt(e[i])); ) i++;
        return 0 <= (s = 0 != s ? s : o.length - e.length);
    }
    return console.log("版本号不能为空"), !1;
}

Page({
    data: {
        d: "00",
        h: "00",
        m: "00",
        s: "00",
        share_status: !1,
        line_index: 1,
        setTimeself: "",
        request: 1,
        showCoupon: !1,
        coupon_succ: !1,
        coupon_index: 0,
        id: 0,
        othersCoupon: !1,
        videoAd: "",
        showAddMeBtn: !1,
        m_height: 60,
        show_single: !0
    },
    adError2: function(t) {
        console.log("原生广告加载失败"), this.setData({
            show_single: !1
        });
    },
    onReady: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        }), wx.getStorageSync("showAddMeFlag") || that.setData({
            showAddMeBtn: !0
        }), wx.createSelectorQuery().select("#global-nav").boundingClientRect(function(t) {
            that.setData({
                m_height: t.height
            });
        }).exec();
    },
    onClickAddToMinProgramCloseBtn: function() {
        wx.setStorageSync("showAddMeFlag", !0), this.setData({
            showAddMeBtn: !1
        });
    },
    onShow: function() {
        var t = {
            action: "details",
            contr: "help",
            token: wx.getStorageSync("token"),
            id: this.data.id
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info), that.clearTime(), that.countdown(), that.loadHelpList();
            var a = wx.getSystemInfoSync();
            t.info.config.video_id && compareV(a.SDKVersion, "2.6.0") && (that.data.videoAd = wx.createRewardedVideoAd({
                adUnitId: t.info.config.video_id
            }), that.data.videoAd.onError(function(t) {
                console.log(t);
            })), wx.createInterstitialAd && that.data.config.screen_id && (that.data.interstitialAd = wx.createInterstitialAd({
                adUnitId: that.data.config.screen_id
            }), that.data.interstitialAd.onError(function(t) {
                console.log(t);
            }), that.data.interstitialAd.show().catch(function(t) {
                console.error(t);
            }));
        });
        var a = wx.getStorageSync("bargain_demo");
        a && (Date.parse(new Date()) - a.time > 1e3 * parseInt(a.demo_time) ? that.confirmBargain() : wx.showModal({
            title: "温馨提示",
            content: "需要试玩" + a.demo_time + "秒才能完成砍价哦",
            showCancel: !1,
            success: function(t) {}
        }), wx.removeStorageSync("bargain_demo"));
    },
    videoAdShow: function() {
        this.data.videoAd && (this.data.videoAd.show().catch(function() {
            that.data.videoAd.load().then(function() {
                return that.data.videoAd.show();
            }).catch(function(t) {
                that.bargain_demo(), wx.showToast({
                    icon: "none",
                    title: "激励视频加载失败~"
                });
            });
        }), that.data.videoAd.offError(function(t) {}), that.data.videoAd.onClose(function(t) {
            that.data.videoAd.offClose(), (t && t.isEnded || void 0 === t) && that.confirmBargain();
        }));
    },
    switchline: function(t) {
        var a = t.currentTarget.dataset.index;
        a != this.data.line_index && (this.setData({
            line_index: a
        }), 1 == a ? this.loadHelpList() : this.loadHelpSucc());
    },
    loadHelpSucc: function() {
        var t = {
            action: "help_succ",
            contr: "help",
            token: wx.getStorageSync("token"),
            id: this.data.goods.id
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
    },
    loadHelpList: function() {
        var t = {
            action: "help_list",
            contr: "help",
            token: wx.getStorageSync("token"),
            id: this.data.id
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
    },
    onLoad: function(t) {
        if (that = this, t.scene) {
            var a = decodeURIComponent(t.scene).split("-");
            this.data.id = a[0], a[1] && wx.setStorageSync("parent_id", a[1]);
        } else this.data.id = t.id, t.parent_id && wx.setStorageSync("parent_id", t.parent_id);
        compareV(wx.getSystemInfoSync().SDKVersion, "2.10.4") || (console.log(2121), this.setData({
            show_single: !1
        })), that.clearTime();
    },
    onUnload: function() {
        this.clearTime();
    },
    clearTime: function() {
        clearTimeout(that.data.setTimeself);
    },
    changeCoupon: function(t) {
        console.log(t), this.setData({
            coupon_index: t.detail.value
        });
    },
    launch: function(t) {
        var a = t.currentTarget.dataset.id;
        if (1 == this.data.request) {
            this.data.request = 2;
            var o = {
                action: "launch",
                contr: "help",
                token: wx.getStorageSync("token"),
                goods_id: a
            };
            _tools2.default.request(o, function(t) {
                that.data.request = 1, 2 == t.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: t.info,
                    showCancel: !1
                }) : wx.reLaunch({
                    url: "/bh_bargain/pages/bargain/bargain?id=" + t.info.id
                });
            });
        }
    },
    countdown: function() {
        if (null != this.data.order && this.data.order) {
            var t = that.data.order.surplus_time;
            that.data.order.surplus_time--;
            var a = parseInt(t / 60 / 60 / 24, 10), o = Math.floor(t / 60 / 60 % 24), e = Math.floor(t / 60 % 60), n = Math.floor(t % 60);
            that.data.order.surplus_time < 0 ? clearTimeout(that.data.setTimeself) : (o < 10 && (o = "0" + o), 
            a < 10 && (a = "0" + a), e < 10 && (e = "0" + e), n < 10 && (n = "0" + n), that.data.setTimeself = setTimeout(function() {
                that.setData({
                    d: a,
                    h: o,
                    m: e,
                    s: n,
                    order: that.data.order
                }), that.countdown();
            }, 1e3));
        } else clearTimeout(that.data.setTimeself);
    },
    gotoRule: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/rule/rule?type=1"
        });
    },
    gotoBuy: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/buy/buy?id=" + this.data.id
        });
    },
    gotoGroup: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/group/group"
        });
    },
    gotoList: function() {
        wx.reLaunch({
            url: "/bh_bargain/pages/goodsList/goodsList"
        });
    },
    showShare: function() {
        this.data.config.dy_bargaining_process_id && wx.requestSubscribeMessage({
            tmplIds: [ this.data.config.dy_bargaining_process_id ],
            success: function(t) {
                console.log(t), that.add_num();
            },
            fail: function(t) {
                console.log(t);
            }
        }), this.setData({
            share_status: !this.data.share_status
        });
    },
    add_num: function() {
        var t = {
            action: "subscribe",
            contr: "my",
            type: 2,
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {});
    },
    gotoCoupon: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/coupon/coupon"
        });
    },
    gotoPoster: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/poster/poster?id=" + this.data.id
        });
    },
    gotoGoods: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/goods/goods?id=" + this.data.goods.id
        });
    },
    showCoupon: function() {
        this.data.is_my ? !this.data.coupon || this.data.coupon.length < 1 ? this.gotoCoupon() : that.statusCoupon() : this.statusOthers();
    },
    statusCoupon: function() {
        this.setData({
            showCoupon: !this.data.showCoupon
        });
    },
    statusOthers: function() {
        this.setData({
            othersCoupon: !this.data.othersCoupon
        });
    },
    useCoupon: function() {
        if (1 == this.data.request) {
            this.data.request = 2;
            var t = {
                action: "useCoupon",
                contr: "help",
                token: wx.getStorageSync("token"),
                order_id: this.data.id,
                coupon_id: this.data.coupon[this.data.coupon_index].id
            };
            _tools2.default.request(t, function(t) {
                that.data.request = 1, 2 == t.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: t.info,
                    showCancel: !1
                }) : (that.setData({
                    coupon_day: that.data.coupon[that.data.coupon_index].worth
                }), that.succ_status(), that.statusCoupon(), that.onShow());
            });
        }
    },
    succ_status: function() {
        this.setData({
            coupon_succ: !this.data.coupon_succ
        });
    },
    a: function() {},
    bargain: function() {
        2 == this.data.config.is_bargain_video ? this.videoAdShow() : 3 == this.data.config.is_bargain_video ? that.bargain_demo() : this.confirmBargain();
    },
    bargain_demo: function() {
        that.data.config.bargain_demo_time && that.data.config.bargain_demo_appid && wx.showModal({
            title: "提示",
            mask: !0,
            content: "试玩" + that.data.config.bargain_demo_time + "秒,完成砍价",
            confirmText: "去试玩",
            success: function(t) {
                t.confirm && wx.navigateToMiniProgram({
                    appId: that.data.config.bargain_demo_appid,
                    success: function(t) {
                        var a = {
                            demo_time: that.data.config.bargain_demo_time,
                            time: Date.parse(new Date())
                        };
                        wx.setStorageSync("bargain_demo", a);
                    },
                    fail: function() {},
                    complete: function() {}
                });
            }
        });
    },
    getUserInfo: function(t) {
        if ("getUserInfo:ok" == t.detail.errMsg) {
            var a = {
                action: "register",
                contr: "my",
                token: wx.getStorageSync("token"),
                encryptedData: t.detail.encryptedData,
                iv: t.detail.iv
            };
            _tools2.default.request(a, function(t) {
                that.bargain();
            });
        }
    },
    confirmBargain: function() {
        if (1 == this.data.request) {
            this.data.request = 2;
            var t = {
                action: "bargain",
                contr: "help",
                token: wx.getStorageSync("token"),
                id: this.data.id
            };
            _tools2.default.request(t, function(t) {
                that.data.request = 1, 2 == t.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: t.info,
                    showCancel: !1
                }) : (wx.showToast({
                    title: "砍价成功"
                }), that.onShow());
            });
        }
    },
    onShareAppMessage: function() {
        return console.log("bh_bargain/pages/newIndex/newIndex?type=2&parent_id=" + this.data.share.member_id + "&id=" + this.data.id), 
        {
            title: this.data.goods.share_txt,
            imageUrl: this.data.goods.cover_image,
            path: "bh_bargain/pages/newIndex/newIndex?type=2&parent_id=" + this.data.share.member_id + "&id=" + this.data.id
        };
    }
});